package com.puyuba.puyuba.view.fragment;

import android.view.View;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.puyuba.puyuba.R;
import com.pyb.java.basemodule.BaseFragment;
import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.cache.bean.UserInfoBean;

import com.puyuba.puyuba.view.viewpager.WrapViewPager;
import com.pyb.java.commonutil.utils.StatusBarUtil;

import java.util.ArrayList;

public class MessageFragment extends BaseFragment {

    private SlidingTabLayout mTabLayout;
    private WrapViewPager mViewPager;
    private String[] mTitles ;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private int id;
    @Override
    public void initView() {
        View viewStatusBar = (View) getActivity().findViewById(R.id.view_status_bar3);
        int screenHeight = StatusBarUtil.getStatusBarHeight(getContext());
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) viewStatusBar.getLayoutParams();
        layoutParams.height = screenHeight;
        viewStatusBar.setLayoutParams(layoutParams);
        mTabLayout = (SlidingTabLayout) getActivity().findViewById(R.id.tl_3);
        mViewPager = (WrapViewPager) getActivity().findViewById(R.id.vp_xiaoxi_page);

        UserInfoBean userInfoBean = CacheUserInfo.getInstance().getsUserInfo();
        if (userInfoBean.getRole()!= null) {
            if (userInfoBean.getRole().getSelrole() == 0) {
                //selrole = 0 判断roletype是内部还是外部   内部的话取sysrole_list的第一个  外部取selrole_list的第一个
                if (userInfoBean.getRole().getRoletype() == 2) {
                    //外部角色
                    id = userInfoBean.getRole().getSelrole_list().get(0).getId();
                } else {
                    //内部角色
                    id = userInfoBean.getRole().getSysrole_list().get(0).getId();
                }
            } else {
                //!=0  代表具体角色
                id = userInfoBean.getRole().getSelrole();
            }

        }
        initFragments();
        initTab();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_message;
    }
    private void initFragments() {
        mFragments.clear();
        mFragments.add(MessageOneFragment.getInstance("消息"));
        mFragments.add(MessageTwoFragment.getInstance("聊天"));
        if (id == 25||id == 30) {
            // 普通会员 学生  除了学生都显示找学生
            mTitles = new String[]{"消息", "聊天", "找顾问"};
            mFragments.add(MessageFindTeacherFragment.getInstance("找顾问"));
        }else {
            mTitles = new String[]{"消息", "聊天", "找学生"};
            mFragments.add(MessageFindStudentFragment.getInstance("找学生"));
        }
    }

    private void initTab() {
        mViewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        mViewPager.setOffscreenPageLimit(2);
        mTabLayout.setViewPager(mViewPager);
        mTabLayout.setOnTabSelectListener(new com.flyco.tablayout.listener.OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

            @Override
            public void onPageSelected(int position) {
                mTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) { }
        });
        mViewPager.setCurrentItem(0);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
